package com.pxxy.app_homepage.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pxxy.app_homepage.adapter.FragmentHomeViewPageAdapter;
import com.pxxy.app_homepage.constant.HomePageConstant;
import com.pxxy.lib_common.base.BaseFragment;
import com.pxxy.lib_common.customview.CustomTitleView;
import com.pxxy.security512.R;

import java.util.ArrayList;
import java.util.List;
@Route(path = "/home/fragmentHome")
public class HomeFragment extends BaseFragment {
    private CustomTitleView homeTitle;
    private TabLayout homeTab;
    private ViewPager homePager;
    private List<BaseFragment> fragments;
    private BaseFragment recommendFragment,hotFragment;

    @Override
    protected int setLayoutID() {
        return R.layout.homepage_fragment_home;
    }

    @Override
    protected void initEvent() {
        //tabLayout连接viewpager
        homeTab.setupWithViewPager(homePager,false);

        //为viewPager设置适配器
        FragmentHomeViewPageAdapter adapter = new FragmentHomeViewPageAdapter(getFragmentManager(),fragments);
        homePager.setAdapter(adapter);

        //设置tabLayout中的内容
        homeTab.getTabAt(0).setText(HomePageConstant.FRAGMENT_HOME_TITLE_TL_RECOMMEND);
        homeTab.getTabAt(1).setText(HomePageConstant.FRAGMENT_HOME_TITLE_TL_HOT);

    }

    @Override
    protected void initData() {
        //把两个fragment放入viewPager中
        fragments.add(recommendFragment);
        fragments.add(hotFragment);

        //创建两个tab
        homeTab.addTab(homeTab.newTab());
        homeTab.addTab(homeTab.newTab());

    }

    @Override
    protected void initView(View view) {
        homeTitle = view.findViewById(R.id.fragment_home_title);
        homeTab = view.findViewById(R.id.fragment_home_tl);
        homePager = view.findViewById(R.id.fragment_home_vp);

        recommendFragment = new HomeRecommendFragment();
        hotFragment = new HomeHotFragment();

        fragments = new ArrayList<>();
    }


}
